home *** CD-ROM | disk | FTP | other *** search
/ Carousel / CAROUSEL.cdr / mactosh / code / p_serlib.sit / Serial Library Source Code / serial.configure.dll.c < prev    next >
C/C++ Source or Header  |  1989-07-27  |  5KB  |  262 lines

  1. /***********************************************************************/
  2. /*    
  3. /*    serial.configure.dll.c
  4. /*    by Atul Butte
  5. /*    Copyright ⌐ 1989 by Microsoft Corporation
  6. /*    All Rights Reserved
  7. /*
  8. /*    version 1.0
  9. /*    
  10. /*    
  11. /*    This CALL/REGISTER will configure a serial port after it has been opened.
  12. /*    
  13. /*    Excel usage:
  14. /*    
  15. /*    = Register( "serial library", "serial.send", "IHHHHH" )
  16. /*    = Call( ref, portNumber, baud, stopbits, parity, data )
  17. /*    
  18. /*    where
  19. /*        portNumber        = number of port (1 = modem, 2 = printer)
  20. /*        baud            = baud rate
  21. /*                            1        = 300 baud
  22. /*                            2        = 600 baud
  23. /*                            3        = 1200 baud
  24. /*                            4        = 1800 baud
  25. /*                            5        = 2400 baud
  26. /*                            6        = 3600 baud
  27. /*                            7        = 4800 baud
  28. /*                            8        = 7200 baud
  29. /*                            9        = 9600 baud
  30. /*                            10        = 19200 baud
  31. /*                            11        = 57600 baud
  32. /*
  33. /*                            300     = 300 baud
  34. /*                            600     = 600 baud
  35. /*                            1200     = 1200 baud
  36. /*                            1800     = 1800 baud
  37. /*                            2400     = 2400 baud
  38. /*                            3600     = 3600 baud
  39. /*                            4800     = 4800 baud
  40. /*                            7200     = 7200 baud
  41. /*                            9600     = 9600 baud
  42. /*                            19200     = 19200 baud
  43. /*                            57600    = 57600 baud
  44. /*        stopbits        = number of stop bits
  45. /*                            1        = 1 stop bit
  46. /*                            2        = 1.5 stop bit
  47. /*                            3        = 2 stop bit
  48. /*
  49. /*                            10        = 1 stop bit
  50. /*                            15        = 1.5 stop bit
  51. /*                            20        = 2 stop bit
  52. /*        parity            = parity
  53. /*                            1        = no parity
  54. /*                            2        = even parity
  55. /*                            3        = odd parity
  56. /*        data            = data bits
  57. /*                            1        = 5 data bits
  58. /*                            2        = 6 data bits
  59. /*                            3        = 7 data bits
  60. /*                            4        = 8 data bits
  61. /*
  62. /*                            5        = 5 data bits
  63. /*                            6        = 6 data bits
  64. /*                            7        = 7 data bits
  65. /*                            8        = 8 data bits
  66. /*    
  67. /***********************************************************************/
  68.  
  69. /***********************************************************************/
  70. /*
  71. /*    D E F I N E S
  72. /*
  73. /***********************************************************************/
  74.  
  75. #define ROUTINE_NAME        "serial.configure"
  76. #define hNIL 0L
  77. #define pNIL 0L
  78.  
  79. /***********************************************************************/
  80. /*
  81. /*    I N C L U D E S
  82. /*
  83. /***********************************************************************/
  84.  
  85. #include "serial.h"
  86. #include "error.h"
  87. #include "get_port.h"
  88.  
  89. /***********************************************************************/
  90. /*
  91. /*    main
  92. /*
  93. /***********************************************************************/
  94.  
  95. pascal short main( port, wBaud, wStopbits, wParity, wData )
  96.  
  97.     unsigned short            port;                    /* serial port to use */
  98.     unsigned short            wBaud;                    /* baud rate */
  99.     unsigned short            wStopbits;                /* stop bits */
  100.     unsigned short            wParity;                /* parity */
  101.     unsigned short            wData;                    /* data bits */
  102.     
  103. {
  104.     register OSErr            err;                    /* result code from Toolbox routines */
  105.     register short            config;                    /* configuration word */
  106.     short                    refIn;                    /* reference number for input port */
  107.     short                    refOut;                    /* reference number for output port */
  108.     SerShk                    flags;
  109.     
  110.     RememberA0();
  111.     SetUpA4();
  112.     
  113.     err = get_port( port, &refIn, &refOut );
  114.     if( err != noErr ) {
  115.         display_error( "Illegal port number." );
  116.         RestoreA4( );
  117.         return( err );
  118.     }
  119.     
  120.     config = 0;
  121.     switch( wBaud ) {
  122.         case 1:
  123.         case 300:
  124.             config |= baud300;
  125.             break;
  126.         case 2:
  127.         case 600:
  128.             config |= baud600;
  129.             break;
  130.         case 3:
  131.         case 1200:
  132.             config |= baud1200;
  133.             break;
  134.         case 4:
  135.         case 1800:
  136.             config |= baud1800;
  137.             break;
  138.         case 5:
  139.         case 2400:
  140.             config |= baud2400;
  141.             break;
  142.         case 6:
  143.         case 3600:
  144.             config |= baud3600;
  145.             break;
  146.         case 7:
  147.         case 4800:
  148.             config |= baud4800;
  149.             break;
  150.         case 8:
  151.         case 7200:
  152.             config |= baud7200;
  153.             break;
  154.         case 9:
  155.         case 9600:
  156.             config |= baud9600;
  157.             break;
  158.         case 10:
  159.         case 19200:
  160.             config |= baud19200;
  161.             break;
  162.         case 11:
  163.         case 57600:
  164.             config |= baud57600;
  165.             break;
  166.         default:
  167.             display_error( "Improper baud rate setting." );
  168.             RestoreA4( );
  169.             return( errParam );
  170.     }
  171.     
  172.     switch( wStopbits ) {
  173.         case 1:
  174.         case 10:
  175.             config |= stop10;
  176.             break;
  177.         case 2:
  178.         case 15:
  179.             config |= stop15;
  180.             break;
  181.         case 3:
  182.         case 20:
  183.             config |= stop20;
  184.             break;
  185.         default:
  186.             display_error( "Improper stop bit setting." );
  187.             RestoreA4( );
  188.             return( errParam );
  189.     }
  190.     
  191.     switch( wParity ) {
  192.         case 1:
  193.             config |= noParity;
  194.             break;
  195.         case 2:
  196.             config |= evenParity;
  197.             break;
  198.         case 3:
  199.             config |= oddParity;
  200.             break;
  201.         default:
  202.             display_error( "Improper parity setting." );
  203.             RestoreA4( );
  204.             return( errParam );
  205.     }
  206.     
  207.     switch( wData ) {
  208.         case 1:
  209.         case 5:
  210.             config |= data5;
  211.             break;
  212.         case 2:
  213.         case 6:
  214.             config |= data6;
  215.             break;
  216.         case 3:
  217.         case 7:
  218.             config |= data7;
  219.             break;
  220.         case 4:
  221.         case 8:
  222.             config |= data8;
  223.             break;
  224.         default:
  225.             display_error( "Improper data bit setting." );
  226.             RestoreA4( );
  227.             return( errParam );
  228.     }
  229.     
  230.     err = SerReset( refOut, config );
  231.     if( err != noErr ) {
  232.         display_error( "Error in configuring output driver." );
  233.         RestoreA4( );
  234.         return( errSerialReset );
  235.     }
  236.     err = SerReset( refIn, config );
  237.     if( err != noErr ) {
  238.         display_error( "Error in configuring input driver." );
  239.         RestoreA4( );
  240.         return( errSerialReset );
  241.     }
  242.     
  243.     flags.fXOn = 0;
  244.     flags.fCTS = 0;
  245.     flags.xOn = 0;
  246.     flags.xOff = 0;
  247.     flags.errs = parityErr | hwOverrunErr | framingErr;
  248.     flags.evts = 0;
  249.     flags.fInX = 0;
  250.     flags.fDTR = 0;
  251.     err = SerHShake( refOut, &flags );
  252.     if( err != noErr ) {
  253.         display_error( "Error in configuring output driver." );
  254.         RestoreA4( );
  255.         return( errSerialReset );
  256.     }
  257.     
  258.     RestoreA4( );
  259.     return( noErr );
  260. }
  261.  
  262. #include "get_port.c"